import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.Timer;


public class GUI{
Timer timer;
JProgressBar progress;	
int bigNum = 0;
int smallNum = 0;

	public void makeGUI(){
		final GUI gui = new GUI();
		//Set up GUI essentials
		JFrame frame = new JFrame();
		JPanel panel = new JPanel(new GridBagLayout());
		Font font = new Font("Arial",Font.BOLD,14);
		Font font1 = new Font("Arial", Font.PLAIN, 14);
		Font font2 = new Font("Arial", Font.BOLD, 18);
		frame.setTitle("Countdown: The Numbers Game");
		progress = new JProgressBar(0,30);
		JLabel novis = new JLabel("");
		JLabel novis2 = new JLabel("");
		JLabel novis3 = new JLabel("");
		JLabel novis4 = new JLabel("");
		
		//Set up panel
		JLabel little = new JLabel("Little Numbers");
		little.setFont(font);
		JLabel big = new JLabel("   Big Numbers    ");
		big.setFont(font);
		//Has to be at least 2 small numbers selected
		Integer[] lNum = {2,3,4,5,6};
		//Can be from 0 - 4 large numbers selected
		Integer[] bNum = {0,1,2,3,4};
		final JComboBox<Integer> littleNumbers = new JComboBox<Integer>(lNum);
		littleNumbers.setFont(font1);
		final JComboBox<Integer> bigNumbers = new JComboBox<Integer>(bNum);
		bigNumbers.setSelectedIndex(4);
		bigNumbers.setFont(font1);
		GridBagConstraints c = new GridBagConstraints();
		//Adding JLabels
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.weighty = 1;
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		little.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(little,c);
		c.gridx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(novis,c);
		c.gridy = 1;
		c.gridx = 2;
		c.fill = GridBagConstraints.HORIZONTAL;
		big.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(big,c);
		c.gridy = 1;
		c.gridx = 3;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(novis2,c);
		//Adding ComboBoxes
		c.gridy = 2;
		c.gridx = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		littleNumbers.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox<Integer> cb = (JComboBox<Integer>)e.getSource();
				int i = (Integer)cb.getSelectedItem();
				if(i == 2)
					bigNumbers.setSelectedIndex(4);
				else if(i == 3)	
					bigNumbers.setSelectedIndex(3);
				else if(i == 4)
					bigNumbers.setSelectedIndex(2);
				else if(i == 5)
					bigNumbers.setSelectedIndex(1);
				else if(i == 6)
					bigNumbers.setSelectedIndex(0);
				smallNum = i;
			}	
			
		});
		panel.add(littleNumbers,c);
		c.gridy = 2;
		c.gridx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(novis3,c);
		c.gridy = 2;
		c.gridx = 2;
		c.fill = GridBagConstraints.HORIZONTAL;
		bigNumbers.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox<Integer> cb = (JComboBox<Integer>)e.getSource();
				int i = (Integer)cb.getSelectedItem();
				if(i == 0)
					littleNumbers.setSelectedIndex(4);
				if(i == 1)
					littleNumbers.setSelectedIndex(3);
				if(i == 2)
					littleNumbers.setSelectedIndex(2);
				if(i == 3)
					littleNumbers.setSelectedIndex(1);
				if(i == 4)
					littleNumbers.setSelectedIndex(0);
				bigNum = i;
			}
			
		});
		panel.add(bigNumbers,c);
		panel.setVisible(true);
		c.gridy = 2;
		c.gridx = 3;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(novis4);
		
		//Pick numbers button
		JButton pick = new JButton("Confirm Selection");
		pick.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
			//Call Joe's method	
				
				
				//Get the numbers
				ArrayList<Integer> arr = gui.pickNumbers();
				//Make the timer
				progress.setValue(30);
				ActionListener listen = new ActionListener(){
					int counter = 30;
					@Override
					public void actionPerformed(ActionEvent arg0) {
						counter--;
						progress.setValue(counter);
						if(counter < 1){
							JOptionPane.showMessageDialog(null, "You are out of time");
							timer.stop();
						}
					}
				};
				timer = new Timer(1000,listen);
				timer.start();
			//Update panel with number returned	
			}
			
			
		});
		pick.setFont(font2);
		c.gridy = 3;
		c.gridx = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(pick,c);
		JLabel chosen = new JLabel("Numbers chosen are:");
		c.gridy = 4;
		c.gridx = 0;
		c.gridwidth = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(chosen,c);
		
		JLabel target = new JLabel("Target number:");
		target.setHorizontalAlignment(SwingConstants.CENTER);
		pick.setFont(font);
		c.gridx = 2;
		c.gridy = 4;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(target,c);
		
		JLabel targetNum = new JLabel("--");
		targetNum.setHorizontalAlignment(SwingConstants.CENTER);
		targetNum.setFont(font);
		c.gridx = 3;
		c.gridy = 4;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(targetNum,c);
		JLabel answer = new JLabel("Your answer:");
		answer.setFont(font1);
		answer.setHorizontalAlignment(SwingConstants.CENTER);
		c.gridx = 0;
		c.gridy = 5;
		c.fill = GridBagConstraints.HORIZONTAL;
		panel.add(answer, c);
		JTextArea userAnswer = new JTextArea();
		c.gridx = 1;
		c.gridy = 5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 3;
		panel.add(userAnswer, c);
		JButton submit = new JButton("Submit answer");
		
		JButton reset = new JButton("Reset");
		JLabel time = new JLabel("Time Left:");
		c.gridx = 0;
		c.gridy = 7;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 1;
		time.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(time,c);
		c.gridx = 1;
		c.gridy = 7;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 3;
		panel.add(progress,c);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		Dimension d = new Dimension(450,400);
		frame.setMinimumSize(d);
		frame.add(panel);
		frame.pack();
	}
	
	public ArrayList<Integer> pickNumbers(){
		SmallNumber small = new SmallNumber();
		BigNumber big = new BigNumber();
		ArrayList<Integer> array = new ArrayList<Integer>();
		int[] littleArr = small.pickLittleNumbers(smallNum);
		int[] bigArr = big.pickBigNumbers(bigNum);
		for(int i = 0; i < littleArr.length; i++)
			array.add(littleArr[i]);
		for(int k = 0; k < bigArr.length; k++)
			array.add(bigArr[k]);
		return array;
	}
	
}
